<template>
  <el-dialog
   v-model="dialogVisible"
   title="编辑"
   width="70%">
    <div class="search-container" v-loading="loading">
      <el-form v-model="searchPage" inline="true" >
        <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="item.cnname">
          <el-select  v-if="item.itemType === 'DICT'" v-model="item.value" :multiple = "item.mutSelect ==='checkBox'">
            <el-option  v-for="(itemChild, itemIndex) in item.dict" :key="itemIndex"  :label="itemChild.value" :value="itemChild.key" />
          </el-select>
          <el-input v-else-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
          <el-col :span="24" v-else>
            <el-form-item>
              <el-date-picker
                v-model="item.dateValue"
                type="date"
                label="Pick a date"
                placeholder="Pick a date"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="data-container">
        <el-table :data="tableData" style="width: 100%" @row-dblclick="selectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="80" label="序号"/>
        <el-table-column prop="diagnose.patient.regNum" label="登记号" width="160" />
        <el-table-column prop="diagnose.patient.name" label="姓名" width="120" />
        <el-table-column prop="typeId" label="样本类型" width="120">
          <template #default="scope">
            <el-select v-model="scope.row.typeId" placeholder="" name="typeId" disabled >
              <el-option value="1" label="血清"/>
              <el-option value="2" label="唾液"/>
              <el-option value="3" label="血浆"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="样本编号" width="120" />
        <el-table-column prop="surplus" label="剩余量" width="100"/>
        <el-table-column prop="serNo" label="流水号" width="200" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
      </el-table>
      <el-pagination
        v-model:currentPage="searchPage.current"
        v-model:page-size="searchPage.pageSize"
        :page-sizes="[20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableDatas.total"
        hide-on-single-page=true
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, onActivated, defineProps, computed, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { list } from '@/api/common/common'
import { dateToString, dateToNext } from '@/utils/date'
const tableDatas = ref({})

const tableData = ref([])

const dialogVisible = computed(() => props.isShowDia)

const emits = defineEmits(['changeRow'])

const props = defineProps({
  isShowDia: {
    type: Boolean,
    default: false
  }
})

const searchPage = ref({
  items: [
    {
      name: 'regNum',
      cnname: '登记号',
      itemType: 'STRING',
      sign: '==',
      value: '',
      dateValue: ''
    },
    {
      name: 'name',
      cnname: '名称',
      itemType: 'STRING',
      sign: 'like',
      value: '',
      dateValue: ''
    },
    {
      name: 'createTime1',
      cnname: '创建开始时间',
      itemType: 'DATE',
      sign: '>=',
      value: dateToString(-7),
      dateValue: dateToNext(-7)
    },
    {
      name: 'createTime2',
      cnname: '结束时间',
      itemType: 'DATE',
      sign: '<=',
      value: dateToString(0),
      dateValue: dateToNext(0)
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const searchFunction = async () => {
  list(searchPage.value, 'grk/sample').then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'name',
        cnname: '名称',
        itemType: 'STRING',
        sign: 'like',
        value: '',
        dateValue: ''
      },
      {
        name: 'createTime1',
        cnname: '开始时间',
        itemType: 'DATE',
        sign: '>=',
        value: dateToString(-7),
        dateValue: dateToNext(-7)
      },
      {
        name: 'createTime2',
        cnname: '结束时间',
        itemType: 'DATE',
        sign: '<=',
        value: dateToString(0),
        dateValue: dateToNext(0)
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

// 双击选中某行数据
const selectionChange = row => {
  console.log(row)
  emits('changeRow', row)
}
</script>

<style>

</style>
